<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="../../bk/css/common.css">
<link rel="stylesheet" href="../../bk/css/main.css">
<script type="text/javascript" src="../../bk/js/jquery.min.js"></script>
<script type="text/javascript" src="../../bk/js/showpic.js"></script>
<script type="text/javascript" src="../../bk/js/ajaxfileupload.js"></script>
<script src="../../ckeditor/ckeditor.js"></script>
<script type="text/javascript">
	
</script>
</head>
<body>
<div id="forms" class="mt10">
	<div class="box">
		<div class="box_border">
			<div class="box_top">
				<b class="pl15">添加商品信息</b>
			</div>
			<div class="box_center">
				<form action="" class="jqtransform" id="addGoodsForm">
					<table class="form_table pt15 pb15" width="100%" border="0" cellpadding="0" cellspacing="0">
						<tr>
							<td class="td_right">商品类型：</td>
							<td>
								<select class="select" name="tid" id="goodsType">
									
								</select>
							</td>
							<td class="td_right">商品名称：</td>
							<td>
								<input type="text" id="gname" name="title" class="input-text lh30" size="40" placeholder="请输入商品名称">
							</td>
							<td class="td_right">商品单价：</td>
							<td>
								<input type="number" id="price" name="price" class="input-text lh30" size="40" placeholder="单价"/>
							</td>
						</tr>
						<tr>
							<td class="td_right">商品库存：</td>
							<td>
								<input type="number" id="balance" name="balance" class="input-text lh30" size="40"  placeholder="库存"/>
							</td>
							<td class="td_right">商品单位：</td>
							<td>
								<input type="text" id="unit" name="unit" class="input-text lh30" size="40" placeholder="单位"/>
							</td>
							<td class="td_right">保质期：</td>
							<td>
								<input type="text" id="qperied" name="qperied" class="input-text lh30" size="40"  placeholder="保质期"/>
							</td>
						</tr>
						<tr>
							<td class="td_right">商品净重：</td>
							<td>
								<input type="text" id="weight" name="weight" class="input-text lh30" size="40" placeholder="净重" >
							</td>
							<td class="td_right">商品图片：</td>
							<td>
								<input type="file" id="pic" name="pic" class="input-text lh30" size="40" multiple="multiple" onchange="setImagePreviews(this,'showPics')">
							</td>
							<td></td>
							<td></td>
						</tr>
						<tr>
							<td>&nbsp;</td>
							<td colspan="5" align="center">
								<div id="showPics">
								
								</div>
							</td>
						</tr>
						<tr>
							<td class="td_right">商品简介：</td>
							<td colspan="5">
								 <textarea name="intro" id="intro" rows="3" cols="100"></textarea>
							</td>
						</tr>
						<tr>
							<td class="td_right">商品描述：</td>
							<td colspan="5">
								 <textarea name="editor1" id="editor1" rows="10" cols="80"></textarea>
							</td>
						</tr>
						<tr>
							<td colspan="4" align="center">
								<input type="button" name="button" class="btn btn82 btn_save2" onclick="addGoodsInfo()" value="添加">
								<input type="reset" name="button" class="btn btn82 btn_res" value="重置">
							</td>
						</tr>
					</table>
				</form>
			</div>
		</div>
	</div>
</div>
<script type="text/javascript">
CKEDITOR.replace( 'editor1' );

function addGoodsInfo() {
	var tno=$('#goodsType option:selected') .val();//选中的值
	tno = parseInt(tno);
	var gname = $.trim($("#gname").val());
	var price = $.trim($("#price").val());
	var balance = $.trim($("#balance").val());
	var descr = $.trim($("#intro").val());
	var intro = CKEDITOR.instances.editor1.getData();
	var unit=$.trim($("#unit").val());
	
	$.ajaxFileUpload({
		url:'../../goodsInfo/addGood',
		secureuri: false,
		fileElementId:"pic",
		dataType:"json",
		data:{gname:gname,tno:tno,price:price,balance:balance,intro:intro,descr:descr,unit:unit},
		success:function(data, stuts) {
			data = parseInt($.trim(data));
			if (data >0) {
				$("#addGoodsForm")[0].reset();
				$("#showPics").html("");
				CKEDITOR.instances.editor1.setData("");
				alert("商品信息添加成功...");
			} else if(data==-2){
				$("#add").removeAttr("disabled");
				alert("说明没有选择上传的图片");
			}else if(data==-3){
				$("#add").removeAttr("disabled");
				alert("请将信息添加完整！！!");
			}else {
				$("#add").removeAttr("disabled");
				alert("商品信息添加失败...");
			}
		},
		error:function(data, status, e) {
			$("#add").removeAttr("disabled");
			alert("商品信息添加失败，请稍后重试...\n" + e);
		}
	});
}


$.post("../../goodsType/findAll",null,function(data){
	var str='';
	for(var i=0;i<data.length;i++){
		str+='<option value="'+data[i].tno+'">'+data[i].tname+'</option>';			
	}
	$("#goodsType").html(str);
},"json");

//重置按钮
function reset(){
	$("#addGoodsForm")[0].reset();
	$("#showPics").html("");
	CKEDITOR.instances.editor1.setData("");
}

</script>
</body>
</html>